package hero_info

import (
	"go_server/game_server/dao"
	"gorm.io/datatypes"
)

// ===================================================================================================================
// 数据库表【hero_info】 DB相关操作
//
// ===================================================================================================================
// 是否包含指定的英雄配置id
func (s *Hero_info_service) ContainsHeroCfgId(roleid int64, herocfgid int32) bool {
	hero := s.LoadByRoleId(roleid)
	return hero.Contains(herocfgid)
}

func (s *Hero_info_service) ContainsHeroCfgIds(roleid int64, herocfgids []int32) (map[int32]bool, bool) {
	hero := s.LoadByRoleId(roleid)
	rstVals := make(map[int32]bool)
	if herocfgids != nil && len(herocfgids) > 0 {
		for i := 0; i < len(herocfgids); i++ {
			tcfgid := herocfgids[i]
			rstVals[tcfgid] = hero.Contains(tcfgid)
		}
	}
	return rstVals, len(rstVals) > 0
}

func (s *Hero_info_service) LoadByRoleId(roleid int64) *Cache_hero_info {
	hero := &Cache_hero_info{}
	if s.isUseRedis {

	} else {
		db := dao.GetHeroInfo(roleid)
		hero.onLoadFromDB(db)
	}
	return hero
}

func (d *Cache_hero_info) onLoadFromDB(db *dao.HeroInfo) {
	d.RoleId = db.RoleId
	d.RoleCells = make(map[int32]*Cache_hero_info_field_cell)

	rolecells := db.RoleCells.Data()
	if rolecells != nil {
		for _, val := range rolecells {
			tmpcell := &Cache_hero_info_field_cell{}
			tmpcell.set_value_db(val)
			d.RoleCells[tmpcell.Serid] = tmpcell
		}
	}

	d.Heros = make(map[int32]*Cache_hero_info_field_hero)
	heros := db.Heros.Data()
	heroskills := db.HeroSkills.Data()
	herocells := db.HeroCells.Data()
	if heros != nil {
		for serid, val := range heros {
			tmphero := &Cache_hero_info_field_hero{SerId: serid}
			tmphero.set_value_db(val)
			tmphero.set_value_db_skills(heroskills[serid])
			tmphero.set_value_db_cells(herocells[serid])
			d.Heros[serid] = tmphero
		}
	}
}
func (d *Cache_hero_info) onSaveToDB(db *dao.HeroInfo) {
	dbRoleCells := make([]string, 0)
	if d.RoleCells != nil {
		for _, cv := range d.RoleCells {
			dbRoleCells = append(dbRoleCells, cv.get_value_db())
		}
	}
	db.RoleCells = datatypes.NewJSONType(dbRoleCells)

	dbHeros := make(map[int32]string)
	dbHeroSkills := make(map[int32][]string)
	dbHeroCells := make(map[int32][]string)
	if d.Heros != nil {
		for serid, hv := range d.Heros {
			dbHeros[serid] = hv.get_value_db()
			dbHeroSkills[serid] = hv.get_value_db_skills()
			dbHeroCells[serid] = hv.get_value_db_cells()
		}
	}
	db.Heros = datatypes.NewJSONType(dbHeros)
	db.HeroSkills = datatypes.NewJSONType(dbHeroSkills)
	db.HeroCells = datatypes.NewJSONType(dbHeroCells)
}

func (d *Cache_hero_info) SaveDB_HeroInfo_All() {
	db := dao.GetHeroInfo(d.RoleId)
	d.onSaveToDB(db)
	dao.UpdateHeroInfo_All(db)
}

func (d *Cache_hero_info) SaveDB_HeroInfo_Heros() {
	db := dao.GetHeroInfo(d.RoleId)
	d.onSaveToDB(db)
	dao.UpdateHeroInfo_Heros(db)
}

func (d *Cache_hero_info) SaveDB_HeroInfo_Cells() {
	db := dao.GetHeroInfo(d.RoleId)
	d.onSaveToDB(db)
	dao.UpdateHeroInfo_Cells(db)
}
